package net.reea.example.multiselect;

import java.util.ArrayList;

import net.reea.utils.LogService;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.example.smallandroidcodetester.R;

public final class MultiSelectionFragment extends SherlockFragment {

	private static final String TAG = MultiSelectionFragment.class.getSimpleName();
	private MultiSelectionListAdapter adapter;
	private ListView list;
	// if ActoinMode is null - assume we are in normal mode
	private ActionMode actionMode;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.list_multi_choice, null);

		list = (ListView) v.findViewById(R.id.MultiChoiceList);
		init(v);

		getSherlockActivity().getSupportActionBar().setHomeButtonEnabled(true);

		return v;
	}

	private void init(View view) {
		// just filling the array with random data
		ArrayList<String> elements = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			elements.add("alma[" + i + "]");
		}
		adapter = new MultiSelectionListAdapter(getActivity(), elements);
		list.setAdapter(adapter);
		list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				if (actionMode != null) {
					// if already in action mode - do nothing
					return false;
				}
				// set checked selected item and enter multi selection mode
				adapter.setChecked(position, true);
				getSherlockActivity().startActionMode(new ActionModeCallback());
				actionMode.invalidate();
				return true;
			}
		});
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (actionMode != null) {
					// if action mode, toggle checked state of item
					adapter.toggleChecked(position);
					actionMode.invalidate();
				} else {
					// do whatever you should on item click
				}
			}
		});
	}

	@Override
	public void onPause() {
		super.onPause();
		if (this.actionMode != null) {
			this.actionMode.finish();
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	private final class ActionModeCallback implements ActionMode.Callback {

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			adapter.enterMultiMode();
			// save global action mode
			actionMode = mode;
			return true;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			// remove previous items
			menu.clear();
			final int checked = adapter.getCheckedItemCount();
			// update title with number of checked items
			mode.setTitle(checked + " Selected");
			switch (checked) {
			case 0:
				// if nothing checked - exit action mode
				mode.finish();
				return true;
			case 1:
				// all items - rename + delete
				getSherlockActivity().getSupportMenuInflater().inflate(R.menu.cab_menu, menu);
				return true;
			default:
				getSherlockActivity().getSupportMenuInflater().inflate(R.menu.cab_menu, menu);
				// remove rename option - because we have more than one selected
				menu.removeItem(R.id.cab_submenu_rename);
				return true;
			}
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, com.actionbarsherlock.view.MenuItem item) {
			switch (item.getItemId()) {
			case R.id.cab_submenu_rename:
				LogService.toast(getActivity(), "CAB RENAME PRESSED");
				return true;
			case R.id.cab_submenu_delete:
				LogService.toast(getActivity(), "CAB DELETE PRESSED");
				return true;
			default:
				LogService.toast(getActivity(), "MEGVAN");
				return false;
			}
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			LogService.toast(getActivity(), "Implement any action you want the done button to do! Selected " + adapter.getCheckedItemCount() + " items!");
			LogService.log(TAG, "Implement any action you want the done button to do!");
			adapter.exitMultiMode();
			// don't forget to remove it, because we are assuming that if it's not null we are in ActionMode
			actionMode = null;
		}

	}

}